package com.takeshequ.main;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalBitmap;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.view.ext.SatelliteMenu;
import com.android.view.ext.SatelliteMenu.SateliteClickedListener;
import com.android.view.ext.SatelliteMenuItem;
import com.takeshequ.Constants;
import com.takeshequ.LoadingActivity;
import com.takeshequ.R;
import com.takeshequ.homepage.HomePageActivity;
import com.takeshequ.publish.PublishActivity;
import com.utils.SharedPreferencesUtils;

public class MenuFragment extends Fragment {

	private ListView listView;
	private static Context context;
	private List<ListItems> mListItems;
	private static LayoutInflater mInflater;
	private ImageView headImageView;
	private TextView nickNameView;
	private static ViewPager viewPager;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	public static MenuFragment newInstance(Context con, ViewPager mp) {
		context = con;
		viewPager = mp;
		mInflater = LayoutInflater.from(context);
		return new MenuFragment();

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.mail_left, container, false);
		View middleView = view.findViewById(R.id.middle);
		LayoutParams layoutParams = middleView.getLayoutParams();
		int width = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 100, getResources()
						.getDisplayMetrics());
		layoutParams.width = getResources().getDisplayMetrics().widthPixels
				- width;
		middleView.setLayoutParams(layoutParams);

		TextView feedsNumberView = (TextView) view
				.findViewById(R.id.main_left_feeds_number);
		feedsNumberView.setText(Constants.member.getUnReadSystemMessageCount());

		TextView fansNumberView = (TextView) view
				.findViewById(R.id.main_left_fans_number);
		fansNumberView.setText(Constants.member.getFansCount());

		headImageView = (ImageView) view.findViewById(R.id.main_left_head_img);
		
		headImageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Constants.selectMemberId=Constants.member.getMemberId();
				startActivity(new Intent(context,HomePageActivity.class));
			}
		});
		FinalBitmap.create(context).display(headImageView,
				Constants.member.getHeadImage());
		SatelliteMenu menu = (SatelliteMenu) view
				.findViewById(R.id.main_left_path_menu);

		List<SatelliteMenuItem> items = new ArrayList<SatelliteMenuItem>();
		items.add(new SatelliteMenuItem(1, R.drawable.main_quick_write));
		items.add(new SatelliteMenuItem(2, R.drawable.main_quick_camera));
		items.add(new SatelliteMenuItem(3, R.drawable.main_quick_place));
		items.add(new SatelliteMenuItem(4, R.drawable.main_quick_face));
		menu.addItems(items);

		menu.setOnItemClickedListener(new SateliteClickedListener() {

			public void eventOccured(int id) {
				Intent intent = new Intent(context, PublishActivity.class);
				intent.putExtra("choose", id);
				startActivity(intent);

			}
		});

		listView = (ListView) view.findViewById(R.id.main_left_listview);
		nickNameView = (TextView) view.findViewById(R.id.main_top_nickName);
		final SharedPreferencesUtils sharedPreferencesUtils = SharedPreferencesUtils
				.getInstance(context);
		nickNameView.setText(Constants.member.getNickName());
		mListItems = new ArrayList<ListItems>();

		// 初始化数据
		LabelItem label1 = new LabelItem("常用");
		mListItems.add(label1);
		//
		Item itemFriend = new Item();
		itemFriend.setResid(R.drawable.main_left_friend);
		itemFriend.setResidText(R.drawable.main_left_friend_text);
		ContentItem contentFriend = new ContentItem(itemFriend);
		contentFriend.setTag("friend");
		mListItems.add(contentFriend);

		//
		Item itemFeeds = new Item();
		itemFeeds.setResid(R.drawable.main_left_feeds);
		itemFeeds.setResidText(R.drawable.main_left_feeds_text);
		ContentItem contentFeeds = new ContentItem(itemFeeds);
		contentFeeds.setTag("feeds");
		mListItems.add(contentFeeds);

		//
		Item itemNearest = new Item();
		itemNearest.setResid(R.drawable.main_left_nearest);
		itemNearest.setResidText(R.drawable.main_left_nearest_text);
		ContentItem contentNearest = new ContentItem(itemNearest);
		contentNearest.setTag("nearest");
		mListItems.add(contentNearest);

		//
		Item itemGroup = new Item();
		itemGroup.setResid(R.drawable.main_left_group);
		itemGroup.setResidText(R.drawable.main_left_group_text);
		itemGroup.setTips(Constants.member.getUnReadMessageCount());
		ContentItem contentGroup = new ContentItem(itemGroup);
		contentGroup.setTag("talkList");
		mListItems.add(contentGroup);

		// 初始化数据
		LabelItem label2 = new LabelItem("推荐应用");
		mListItems.add(label2);

		//
		Item itemCarKeeper = new Item();
		itemCarKeeper.setResid(R.drawable.main_left_carkeeper);
		itemCarKeeper.setResidText(R.drawable.main_left_carkeeper_text);
		ContentItem contentCarKeeper = new ContentItem(itemCarKeeper);
		contentCarKeeper.setTag("carkeeper");
		mListItems.add(contentCarKeeper);

		// 初始化数据
		LabelItem label3 = new LabelItem("操作");
		mListItems.add(label3);

		//
		Item itemPerson = new Item();
		itemPerson.setResid(R.drawable.main_left_homepage);
		itemPerson.setResidText(R.drawable.main_left_homepage_text);
		ContentItem contentPerson = new ContentItem(itemPerson);
		contentPerson.setTag("homepage");
		mListItems.add(contentPerson);

		Item itemSignOut = new Item();
		itemSignOut.setResid(R.drawable.main_left_signout);
		itemSignOut.setResidText(R.drawable.main_left_signout_text);
		ContentItem contentSignOut = new ContentItem(itemSignOut);
		contentSignOut.setTag("signout");
		mListItems.add(contentSignOut);
		PartAdapter adapter = new PartAdapter();
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int arg2,
					long arg3) {
				((MenuActivity) getActivity()).getSlideoutHelper().close();
				String tag = ((ContentItem) mListItems.get(arg2)).getTag();
				System.out.println("tag:" + tag);
				if ("friend".equals(tag)) {
					viewPager.setCurrentItem(0);
				} else if ("talkList".equals(tag)) {
					viewPager.setCurrentItem(3);
				} else if ("nearest".equals(tag)) {
					viewPager.setCurrentItem(2);

				} else if ("feeds".equals(tag)) {
					viewPager.setCurrentItem(1);
				} else if ("signout".equals(tag)) {
					sharedPreferencesUtils.setMemberId("");
					Intent intent = new Intent(context, LoadingActivity.class);
					startActivity(intent);

				} else if ("homepage".equals(tag)) {
					Constants.selectMemberId = Constants.memberId;
					Intent intent = new Intent(context, HomePageActivity.class);
					startActivity(intent);
				} else if ("carkeeper".equals(tag)) {
					Intent it = new Intent(Intent.ACTION_VIEW, Uri
							.parse("www.365take.com/download/apk/ERC_Car.apk"));
					it.setClassName("com.android.browser",
							"com.android.browser.BrowserActivity");
					startActivity(it);
				}

			}
		});

		return view;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	class PartAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mListItems.size();
		}

		@Override
		public Object getItem(int position) {
			return mListItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public boolean isEnabled(int position) {
			return mListItems.get(position).isClickable();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			return mListItems.get(position).getView(context, convertView,
					mInflater);
			// view.setTag( ((ContentItem)mListItems.get(position)).setTag(tag))
			// return view;
		}
	}

}
